import { getConfig } from '../config';
import { COLORS } from '../constant';

const colorStore = () => ({
    red: '#f00',
    white: '#fff',
    black: '#000',
    blue: '#00f',
    transparent: 'transparent',
    ...getConfig(COLORS)
});

export const getColors = () => {
    return colorStore();
}

export const getColorsKey = () => {
    return Object.keys(colorStore());
}

const radix16 = (value: string) => {
    return parseInt(value, 16);
}

export const textToRgbText = (str: any, opacity = 1) => {
    const hex = /^#?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(str)
        ? str.replace(/^#/, '')
        : colorStore()[str].replace(/^#/, '')
    
    if (hex === 'transparent') {
        return 'transparent';
    }

    if (hex.length === 6) {
        const reg = /[a-fA-F0-9]{2}/g;
        return 'rgba(' + hex
            .match(reg)
            .map(radix16)
            .join(',') + 
            `,${opacity})`
    }

    if (hex.length === 3) {
        return 'rgba(' + hex
            .split('')
            .map((x: string) => radix16(x.repeat(2)))
            .join(',') + 
            `,${opacity})`
    }

    if (hex.length === 8) {
        const reg = /[a-fA-F0-9]{2}/g;
        let [r, g, b, a] = hex.match(reg);
        a = Number(Number(Math.round(parseInt(a, 16)) / 255).toFixed(2));
        return 'rgba(' + [r, g, b]
            .map(radix16)
            .join(',') +
            `,${a})`;
    }

    return '';
}